<!-- 回款审核or作废弹窗 -->
<template>
  <div class='returned_money_modal'>
    <div class='return_back'>
      <Form ref="formValidateReturnedMoney" :model="formValidateReturnedMoney"
        :rules="ruleValidateReturnBack" :label-width="150">
        <div v-if="returnedMoneyModaltype==1">
          <Row :gutter="16">
            <Col span="22">
            <FormItem label="审核结果：" prop="type">
              <Select v-model="formValidateReturnedMoney.type" placeholder="请选择"
                class="cityConfigArea_moudle_select">
                <Option v-for="item in resultAttrSort" :key="item.key" :value="item.key">
                  {{ item.value }}</Option>
              </Select>
            </FormItem>
            </Col>
          </Row>
          <Row :gutter="16" v-if="formValidateReturnedMoney.type==1">
            <Col span="22">
            <FormItem label="入账日期：" prop="creditDate">
            <DatePicker type="date" :options="Approach" :value="formValidateReturnedMoney.creditDate" :clearable='false'
             @on-change='formValidateReturnedMoney.creditDate = $event' format="yyyy-MM-dd"></DatePicker>
            </FormItem>
            </Col>
          </Row>
          <Row :gutter="16" v-if="formValidateReturnedMoney.type==2">
            <Col span="22">
            <FormItem label="驳回原因：" prop="ReturnedMoneyMess">
              <Input v-model="formValidateReturnedMoney.ReturnedMoneyMess" :maxlength="300"
                type="textarea" :autosize="{minRows: 3,maxRows: 5}"
                placeholder="请填写驳回原因"></Input>
            </FormItem>
            </Col>
          </Row>
        </div>
        <div v-else>
          <Row :gutter="16">
            <Col span="22">
            <FormItem label="请填写作废原因：" prop="ReturnedMoneyDelMess">
              <Input v-model="formValidateReturnedMoney.ReturnedMoneyDelMess" :maxlength="300"
                type="textarea" :autosize="{minRows: 3,maxRows: 5}" placeholder=""></Input>
            </FormItem>
            </Col>
          </Row>
        </div>
      </Form>
      <div class="flex flex-horizontal-end btn-operate">
        <Button @click="handleClose">取消</Button>
        <Button type="primary" :loading="loading" @click="submitReturnedMoneyFun()"
          style="margin-left: 8px">确定</Button>
      </div>
    </div>
  </div>
</template>

<script>
  import selectPayBackApi from '@/biz/paymenManage/selectPayBackList'
  import moment from 'moment'
  export default {
    props: {
      returnedMoneyModaltype: {
        type: Number,
        default: null,
      },
      rowsData: {
        type: Object,
        default: () => {
          return {}
        },
      },
    },
    data() {
      return {
        loading: false,
        Approach: {
          disabledDate(date) {
            return date && date.valueOf() > Date.now()
          },
        },
        resultAttrSort: [
        {
          key: 1,
          value: '回款确认',
        },
        {
          key: 2,
          value: '回款驳回',
        }, ],
        formValidateReturnedMoney: {
          type: null,
          ReturnedMoneyMess: '',
          ReturnedMoneyDelMess: '',
          creditDate:moment(new Date()).format('YYYY-MM-DD')
        },
        ruleValidateReturnBack: {
          type: [
          {
            required: true,
            type: 'number',
            message: '请选择审核结果',
            trigger: 'change',
          }, ],
          ReturnedMoneyMess: [
            { required: true, message: '请填写驳回原因', trigger: 'change' },
          ],
          creditDate:[{required: true, message: '请选择入账日期', trigger: 'change'}],
          ReturnedMoneyDelMess: [
            { required: true, message: '请填写作废原因', trigger: 'change' },
          ],
        },
      }
    },
    methods: {
      handleClose() {
        this.$emit('onClose')
      },
      submitReturnedMoneyFun() {
        this.$refs.formValidateReturnedMoney.validate((valid) => {
          if (valid) {
            console.log(this.formValidateReturnedMoney)
            if (this.returnedMoneyModaltype == 2) {
              //回款作废
              this.ReturnedMoneyDel(this.formValidateReturnedMoney)
            } else {
              //回款确认、回款驳回
              this.loading = true
              let params = {}
              if (this.formValidateReturnedMoney.type == 1) {
                params = {
                  receiptRecordId: this.rowsData.receiptRecordId,
                  type: this.formValidateReturnedMoney.type,
                  creditDate:this.formValidateReturnedMoney.creditDate,
                  remark: '',
                }
              } else {
                params = {
                  receiptRecordId: this.rowsData.receiptRecordId,
                  type: this.formValidateReturnedMoney.type,
                  remark: this.formValidateReturnedMoney.type == 2 ?
                    this.formValidateReturnedMoney.ReturnedMoneyMess : '',
                }
              }
              selectPayBackApi.paymentConfirmation(params).then((res) => {
                this.loading = false
                this.$Message.success('审核成功!')
                this.$emit('onClose')
                this.$emit('onChangeTable')
              }).catch(() => {
                this.loading = false
              })
            }
          }
        })
      },
      ReturnedMoneyDel(obj) {
        console.log(obj)
        let that = this
        this.$Modal.confirm({
          title: '回款作废',
          content: `是否确认作废该笔回款？作废后，系统将自动为顾问创建1条的回款任务`,
          okText: '确定',
          cancelText: '取消',
          onOk: () => {
            this.loading = true
            let params = {
              receiptRecordId: this.rowsData.receiptRecordId,
              type: 3,
              remark: obj.ReturnedMoneyDelMess,
            }
            selectPayBackApi.paymentConfirmation(params).then((res) => {
              this.loading = false
              that.$Message.success('作废成功!')
              this.$emit('onClose')
              this.$emit('onChangeTable')
            }).catch(() => {
              this.loading = false
            })
          },
        })
      },
    },
    mounted() {
      console.log(this.rowsData, 'rowdata')
    },
  }
</script>
<style lang='scss' scoped>
</style>